<template>
	<view class="container">
		<!-- 蓝色背景区域 -->
		<view class="blue-bg"></view>
		
		<!-- 内容区域 -->
		<view class="content">
			<!-- 地图外层卡片 -->
			<view class="map-wrapper">
				<!-- 地图卡片 -->
				<view class="map-card">
					<map 
						class="map"
						:latitude="latitude"
						:longitude="longitude"
						:markers="markers"
						:scale="14"
						show-location
						:show-compass="true"
						:enable-rotate="true"
						type="gcj02"
					></map>
				</view>
			</view>
		</view>
		
		<!-- 底部信息卡片 -->
		<view class="bottom-card">
			<view class="shop-info">
				<text class="shop-name">{{shopName}}</text>
				<text class="shop-address">{{shopAddress}}</text>
			</view>
			<button class="nav-btn" @click="startNavigation">导航</button>
		</view>
	</view>
</template>

<script>
import amap from '@/utils/amap.js'

export default {
	data() {
		return {
			shopName: '',
			shopAddress: '',
			latitude: 0,
			longitude: 0,
			markers: []
		}
	},
	onLoad(options) {
		// 获取传递的参数
		this.shopName = options.name
		this.shopAddress = options.address
		this.latitude = parseFloat(options.latitude)
		this.longitude = parseFloat(options.longitude)
		
		// 设置目标位置标记
		this.markers = [{
			id: 1,
			latitude: this.latitude,
			longitude: this.longitude,
			iconPath: '/static/位置 拷贝 4@2x.png',
			width: 24,
			height: 24,
			callout: {
				content: this.shopName,
				padding: 8,
				borderRadius: 4,
				display: 'ALWAYS',
				textAlign: 'center',
				fontSize: 12,
				bgColor: '#FFFFFF',
				color: '#333333'
			}
		}]
	},
	methods: {
		goBack() {
			uni.navigateBack()
		},
		startNavigation() {
			uni.showActionSheet({
				itemList: ['腾讯地图导航', '高德地图导航'],
				success: (res) => {
					switch (res.tapIndex) {
						case 0: // 腾讯地图
							this.openTencentMap();
							break;
						case 1: // 高德地图
							this.openAMap();
							break;
					}
				}
			});
		},
		// 打开腾讯地图
		openTencentMap() {
			// 检查是否安装腾讯地图
			if (wx.navigateToMiniProgram) {
				wx.navigateToMiniProgram({
					appId: 'wx5bc2ac602a747594', // 腾讯地图小程序的 appId
					path: `pages/navigation/navigation?destination=${this.latitude},${this.longitude}&key=${this.shopName}`,
					fail: () => {
						// 如果跳转失败，使用内置导航
						this.useBuiltInNavigation();
					}
				});
			} else {
				this.useBuiltInNavigation();
			}
		},
		// 打开高德地图
		openAMap() {
			// 检查是否安装高德地图
			if (wx.navigateToMiniProgram) {
				wx.navigateToMiniProgram({
					appId: 'wx33c8fa39740aa7d7', // 高德地图小程序的 appId
					path: `pages/route/route?destination=${this.latitude},${this.longitude}&destName=${this.shopName}`,
					fail: () => {
						// 如果跳转失败，使用内置导航
						this.useBuiltInNavigation();
					}
				});
			} else {
				this.useBuiltInNavigation();
			}
		},
		// 使用微信内置导航
		useBuiltInNavigation() {
			uni.openLocation({
				latitude: this.latitude,
				longitude: this.longitude,
				name: this.shopName,
				address: this.shopAddress,
				scale: 18
			});
		}
	}
}
</script>

<style>
.container {
	min-height: 100vh;
	background: #f5f5f5;
	position: relative;
}

.blue-bg {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	height: 360rpx;
	background-color: #2979ff;
}

.content {
	position: relative;
	z-index: 1;
	padding: 32rpx;
	height: 100%;
}

.map-wrapper {
	width: 100%;
	height: calc(100vh - 180rpx); /* 增加底部卡片的高度 */
	background: #fff;
	border-radius: 20rpx 20rpx 0rpx 0rpx;
	padding: 24rpx;
	box-sizing: border-box;
}

.map-card {
	width: 100%;
	height: 100%;
	border-radius: 16rpx 16rpx 0rpx 0rpx;
	overflow: hidden;
	position: relative;
}

.map {
	width: 100%;
	height: 100%;
}

.bottom-card {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	height: 180rpx;
	background: #fff;
	padding: 32rpx 32rpx 48rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
	box-sizing: border-box;
	z-index: 999;
}

.shop-info {
	flex: 1;
}

.shop-name {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 16rpx; /* 增加间距 */
	display: block;
}

.shop-address {
	font-size: 28rpx;
	color: #666;
	line-height: 1.4; /* 添加行高 */
}

.nav-btn {
	width: 160rpx;
	height: 72rpx;
	line-height: 72rpx;
	background: #2979ff;
	color: #fff;
	font-size: 32rpx;
	border-radius: 40rpx;
	text-align: center;
	border: none;
	padding: 0;
	margin: 0;
	margin-left: 32rpx;
}
</style>
